package ru.ifmo.ctd.intsys.afanasyeva.boosting;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import ru.ifmo.ctd.intsys.afanasyeva.neural.digits.ImageUtils;

public class MainSingle {
	public static void main(String[] args) throws IOException {
		String path = "data/sample/";
		File[] samples = (new File(path)).listFiles();
		int[] samplesValues = new int[samples.length];
				
		double[][] inputs = new double[samples.length][];
		double[] answers = new double[samples.length];
		Arrays.fill(answers, -1);
		
		for (int i = 0; i < samples.length; i++) {
			inputs[i] = ImageUtils.imageToBits(samples[i].getPath(), 3, 5);
			samplesValues[i] = Integer.parseInt(samples[i].getName().substring(0, 1));
		}
		
		
		Perceptron perc = train(8, inputs, samplesValues);
		
		
		for (int i = 0; i < samples.length; i++) {
			System.out.println(samples[i].getName());
			//for (int j = 0; j < 10; j++) {
				System.out.print(perc.getOutput(inputs[i]) + "  ");
			//}
			System.out.println();
		}
	}
	
	public static Perceptron train(int pattern, double[][] inputs, int[] samplesValues) {
		double[] answers = new double[inputs.length];
		Arrays.fill(answers, -1);
		
		for (int i = 0; i < inputs.length; i++) {
			if (samplesValues[i] == pattern) {
				answers[i] = 1;
			}
		}			
		Perceptron perc = new Perceptron(answers.length, 0.9);
		perc.train(inputs, answers, 10000);
		return perc;
	}
}
